﻿using System.Collections.Generic;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.Logic;
using NetProtocol.POD;

namespace IQIGame.Onigao.GamePlay
{
    public class SummonResponse : ISerializeCustomPOD, IReusableClassWithFree
    {
        public BattleUnitViewPOD unit { get; protected set; }
        public int summonerGuid { get; protected set; }

        public void SetUnit(Unit self, Unit summoner)
        {
            this.unit = self.GeneratePOD() as BattleUnitViewPOD;
            this.summonerGuid = summoner.GUID;
        }
        
        public virtual void Serialize(SOutputStream _buf)
        {
            _buf.Write_int(this.unit.guid);
            _buf.Write_long(this.unit.pid);
            _buf.Write_int(this.unit.configID);
            _buf.Write_int(this.unit.unitType);
            _buf.Write_int(this.unit.level);
            _buf.Write_int(this.unit.radius);
            _buf.Write_float(this.unit.pos.x);
            _buf.Write_float(this.unit.pos.y);
            _buf.Write_float(this.unit.forward.x);
            _buf.Write_float(this.unit.forward.y);
            _buf.Write_int(this.unit.weaknessPoint);
            _buf.Write_int(this.unit.weaknessType);
            SerializeCustomPODUtil.SerializeListInt(_buf, this.unit.attr);
            SerializeCustomPODUtil.SerializeListInt(_buf, this.unit.skill);
            _buf.Write_int(this.summonerGuid);
        }

        public virtual void DeSerialize(SInputStream _buf)
        {
            this.unit = new BattleUnitViewPOD();
            this.unit.guid = _buf.Read_int();
            this.unit.pid = _buf.Read_long();
            this.unit.configID = _buf.Read_int();
            this.unit.unitType = _buf.Read_int();
            this.unit.level = _buf.Read_int();
            this.unit.radius = _buf.Read_int();
            this.unit.pos = new Vector2POD(_buf.Read_float(), _buf.Read_float());
            this.unit.forward = new Vector2POD(_buf.Read_float(), _buf.Read_float());
            this.unit.weaknessPoint = _buf.Read_int();
            this.unit.weaknessType = _buf.Read_int();
            var rAttrList = new List<int>();
            SerializeCustomPODUtil.DeSerializeListInt(_buf, ref rAttrList);
            this.unit.unsafe_attr = rAttrList;
            var rSkillList = new List<int>();
            SerializeCustomPODUtil.DeSerializeListInt(_buf, ref rSkillList);
            this.unit.unsafe_skill = rSkillList;
            this.summonerGuid = _buf.Read_int();
        }

        public uint MaxStore => 3;

        public virtual void OnReset()
        {
            this.unit = null;
            this.summonerGuid = 0;
        }

        public virtual void Free()
        {
            ClassPool.Put(this);
        }
    }
}